package com.myblog.demo.common;

import org.springframework.util.StringUtils;

/**
 * 字符串内容截取工具类
 */
public class StringTools {

    // 默认长度
    private static final int DEFAULT_LENGTH = 300;

    public static String subLength(String content) {
        if (!StringUtils.hasLength(content)) {
            return content;
        }
        if (content.length() <= DEFAULT_LENGTH) {
            return content;
        }
        return content.substring(0, DEFAULT_LENGTH) + " ... ";
    }

    public static String subLength(String content, int maxLength) {
        if (!StringUtils.hasLength(content) || maxLength <= 0) {
            return content;
        }
        if (content.length() <= maxLength) {
            return content;
        }
        return content.substring(0, maxLength) + " ... ";
    }
}
